package com.wondertek.service.mapper.business;

import com.wondertek.common.base.Result;
import com.wondertek.service.dto.app.req.HomePageDateReq;
import com.wondertek.service.dto.app.res.HomePageRoomRes;
import com.wondertek.service.dto.app.res.HomePageTopRes;
import com.wondertek.service.dto.app.res.HomePageTrendRes;
import com.wondertek.service.dto.app.res.HomePageTypeRes;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author wangyu
 */
public interface HomePageMapper {

    @Select("select count(if(date(error_time) = CURDATE(),1,null)) as reportTodaySum,count(if(is_read=0,1,null)) as pendingSum from device_error_record where type in (1,2,3,4) and device_id in(select (select sn_code from device d where d.id = device_id) from rental_device  where consumer_id = #{consumerId} and is_delete = 0 and end_date > now())")
    HomePageTopRes top(@Param("consumerId") String consumerId);

//    @Select("select (select device_code from device where sn_code = device_id and is_delete = 0) from device_error_record where device_id in(select (select sn_code from device d where d.id = device_id) from rental_device  where consumer_id = #{consumerId} and is_delete = 0 and end_date > now()) and date(error_time) = CURDATE() group by device_id")
//    List<String> topDeviceCode(@Param("consumerId") String consumerId);

    @Select("select status from device where is_delete = 0 and id in (select device_id from rental_device where is_delete =0 and end_date > now() and consumer_id = #{consumerId})")
    List<Integer> topDeviceCode(@Param("consumerId") String consumerId);

    @Select("select status from accessory where is_delete = 0  and room_id in( select room_id from rental_device where is_delete =0 and end_date > now() and consumer_id = #{consumerId})")
    List<Integer> topAccessoryCode(@Param("consumerId") String consumerId);

    /**
     * 首页房间控制
     * @return
     */
    @Select("select room_id,r.mode,r.room_name,r.device_id,d.device_name,d.site_id,s.site_name,r.room_code,(select sn_code from device where id = r.device_id) as snCode from rental_device rd left join room r on rd.room_id = r.id left join device d on r.device_id = d.id left join site s on d.site_id = s.id where consumer_id = #{consumerId} and rd.is_delete = 0 and rd.end_date > now() and rd.room_id is not null and rd.room_id != ''")
    List<HomePageRoomRes> findRoom(@Param("consumerId") String consumerId);

    /**
     * 首页报警趋势
     * @param homePageDateReq
     * @return
     */
    @Select("select date,count(1) as num from ( select id,date(error_time) date from device_error_record der where type in (1,2,3,4) and exists( select 1 from rental_device rd left join device d on rd.device_id = d.id where rd.is_delete = 0 and rd.end_date > now() and d.sn_code = der.device_id and rd.consumer_id = #{consumerId} ) and error_time >= #{startDate} and error_time <= #{endDate}) a group by a.date order by date")
    List<HomePageTrendRes> findTrend(HomePageDateReq homePageDateReq);

    /**
     * 首页类型分布
     *
     * @param homePageDateReq
     * @return
     */
    @Select("select a.type as error_type,ifnull(b.num,0) as num from (select 'Alarm' as type from dual union select 'Events' from dual union select 'Error' from dual union select 'Notification' from dual)  a left join (select case type when 1 then'Alarm' when 2 then 'Error' when 3 then 'Events' when 4 then 'Notification' end as type,count(a.id) num from device_error_record a where exists( select 1 from rental_device rd left join device d on rd.device_id = d.id where rd.is_delete = 0 and rd.end_date > now() and d.sn_code = a.device_id and rd.consumer_id = #{consumerId} ) and error_time >= #{startDate} and error_time <= #{endDate} group by type) b on a.type=b.type")
    List<HomePageTypeRes> findTypeRatio(HomePageDateReq homePageDateReq);

}
